const config = require('../config')
const axios = require('axios')
const pako = require('pako')
const queryString = require('querystring')

async function transfer(ctx) {
    const {
        request: req,
        response: res
    } = ctx
    // base64 解密
    const json = new Buffer(req.body.data_list, 'base64').toString()
    // gzip 加密
    const buffer = Buffer.from(pako.gzip(json, {
        to: 'string'
    }))
    const data_list = buffer.toString('base64')
    // 发送base64
    await axios.post(config.EventUrl, queryString.stringify({
        data_list: data_list
    }), {
        headers: {
            'Accept-Encoding': 'gzip',
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    }).then(res => {
        console.log('res\n', res);
        ctx.body = {
            code: 200
        }
    }).catch(error => {
        if (error.response) {
            ctx.body = error.response.data
        } else {
            ctx.body = {
                code: 777,
                message: '未知错误'
            }
        }
    })
}

module.exports = {
    transfer
}